package com.stx.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.stx.biz.UserBiz;
import com.stx.biz.impl.UserBizImpl;
import com.stx.entity.User;

/**
 * Servlet implementation class UserServlet
 */
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private UserBiz userBiz = new UserBizImpl();  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if("isName".equals(method)){
			doUserName(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	protected void doUserName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uname = request.getParameter("uname");
		
//		boolean result = userBiz.isUserName(uname);
		User user = userBiz.checkUserName(uname);
		PrintWriter out = response.getWriter();
		String userinfo=null;
		if(user!=null){
		  ObjectMapper mapper = new ObjectMapper();
		  userinfo = mapper.writeValueAsString(user);
//		  String suser = "{\"userId\":"+user.getUserId()+",\"username\":\""+user.getUserName()+"\"}";
		 
		}
		out.print(userinfo);
		out.flush();
		out.close();
		
	}

}
